package com.itheima.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.vo.MyUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.EnableMBeanExport;
import org.springframework.web.bind.annotation.*;

import javax.xml.transform.Result;
import java.util.List;

@RestController
public class DishController {

    @Autowired
    private DishService dishService;

    /**
     * 查询所有   按名称查询
     *
     * @param pageNum  第几页
     * @param pageSize 每页几条数据
     * @param name     查询名称
     * @returnprice
     */
    @GetMapping("/dish/page")
    public ResultInfo findAll(@RequestParam(value = "page", defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "10") Integer pageSize,
                              String name) {
        Page<Dish> page = dishService.findByPage(pageNum, pageSize, name);
        return ResultInfo.success(page);
    }

    /**
     * 添加菜品
     *
     * @param dish dish实体定义了dishFlavors属性，可以接收前端传入的所有参数
     * @return
     */
    @PostMapping("/dish")
    public ResultInfo add(@RequestBody Dish dish) {
        dishService.add(dish);
        return ResultInfo.success(null);
    }


    @GetMapping("/dish/{id}")
    public ResultInfo findById(@PathVariable Long id){
        Dish dish = dishService.findById(id);
        return ResultInfo.success(dish);
    }

    @PutMapping("/dish")
    public ResultInfo update(@RequestBody Dish dish){
        dishService.update(dish);
        return ResultInfo.success(null);
    }

//    批量停售  启售
    @PostMapping("/dish/status/{status}")
    public ResultInfo stopAndStartSale(@PathVariable("status") Integer status , MyUtil myUtil){
        dishService.stopAndStartSale(status,myUtil);
        return ResultInfo.success(null);
    }


//  批量删除
    @DeleteMapping("/dish")
    public ResultInfo delete(MyUtil myUtil){
        dishService.deleteByIds(myUtil);
        return ResultInfo.success(null);
    }

    @GetMapping("/dish/list")
    public ResultInfo findByCategoryId(@RequestParam(value = "categoryId",required = false) Long id,
                                       @RequestParam(value = "name",required = false) String name){
        if(StringUtils.isEmpty(name)){
            List<Dish> list = dishService.findByCategoryId(id);
            return ResultInfo.success(list);
        }else {
            List<Dish> list = dishService.findByName(name);
            return ResultInfo.success(list);
        }
    }




}
